package com.hqyj.zss.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.zss.dao.EmployeeDao;
import com.hqyj.zss.pojo.Employee;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    //查询所有员工
    @Override
    public PageData<Employee> queryAllEmployee(int pageNumber,int pageSize,String e_name) {
        //使用PageHelper静态方法，指定页数、每页记录数，此时开始拦截mybatis下一次数据库查询操作
        PageHelper.startPage(pageNumber, pageSize);
        List<Employee> userList = employeeDao.queryAllEmployee(e_name);
        PageInfo<Employee> pageInfo = new PageInfo<>(userList);
        PageData<Employee> pageData = new PageData<>();
        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);

        if(pageInfo.isHasNextPage()){
            pageData.setNextPage(pageNumber + 1);
        }else{
            pageData.setNextPage(pageNumber);
        }
        if(pageInfo.isHasPreviousPage()){
            pageData.setPreviousPage(pageNumber - 1);
        }else{
            pageData.setPreviousPage(pageNumber);
        }
        pageData.setTotalPage(pageInfo.getPages());
        pageData.setTotalSize((int)pageInfo.getTotal());
        pageData.setList(pageInfo.getList());
        return pageData;
    }

    //新增员工
    @Override
    public int addEmployee(Employee employee) {
        return employeeDao.addEmployee(employee);
    }

    //修改员工信息
    @Override
    public int updateEmployee(Employee employee) {
        return employeeDao.updateEmployee(employee);
    }

    //删除员工
    @Override
    public int deleteEmployee(int e_id) {
        return employeeDao.deleteEmployee(e_id);
    }

    @Override
    public Employee queryOneEmployee(int e_id) {
        return employeeDao.queryOneEmployee(e_id);
    }

}
